package com.javase.多线程.线程的创建;

import org.junit.jupiter.api.Test;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Auther: liyanhao
 * @Description: 线程创建测试方法
 * @Date: 2022/5/14 15:42
 * @Version: v1.0
 */
public class MainTest {
    @Test
    public void TestCallable() throws ExecutionException, InterruptedException {
        //1.创建一个类实现Callable接口，实现call方法。这个接口类似于Runnable接口，
        // 但比Runnable接口更强大，增加了异常和返回值
        //2.创建一个FutureTask，指向Callable对象，作为线程任务；
        //3.创建线程，指定线程任务；
        //4.启动线程
        FutureTask futureTask = new FutureTask(new MyCallableThread());
        new Thread(futureTask,"t1").start();
        System.out.println(futureTask.get());
    }
}



class MyCallableThread implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        System.out.println("Callable创建线程");
        return 200;
    }
}

